﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AvalonDock;
using System.Diagnostics;
using IllSoft.Xvcc.Volano.Client;
using System.Net;

namespace IllSoft.Xvcc.WpfUI
{
    /// <summary>
    /// Interaction logic for ServerWindow.xaml
    /// </summary>
    public partial class ServerWindow : DocumentContent, IChatWindow
    {
        #region IChatWindow Members

        public event EventHandler<LogEventArgs> Log;

        public event EventHandler<NewDocumentEventArgs> NewDocumentRequired;

        #endregion

        public ServerWindow()
        {
            InitializeComponent();

            this._volanoClient = new VolanoClient();

            this._volanoClient.Connected += new EventHandler(_volanoClient_Connected);
            this._volanoClient.ChannelListReceived += new EventHandler<ChannelListEventArgs>(_volanoClient_ChannelListReceived);
            this._volanoClient.UserListReceived += new EventHandler<UserListEventArgs>(_volanoClient_UserListReceived);

            #region TODO: Manually set Connection Args -> NEEDS TO BE REMOVED AND LOADED FROM EXTERNAL CONFIGURATION FILE!!!

            ConnectionArgs connectionArgs = new ConnectionArgs();

            // Initialize connection arguments
            // elchat.com
            //IPEndPoint targetHost = new IPEndPoint(Dns.GetHostAddresses("75.125.71.50")[0], 8000);

            // Regionenchat
            IPEndPoint targetHost = new IPEndPoint(Dns.GetHostAddresses("chat.chat4free.de")[0], 8002);

            // Themenchat
            //IPEndPoint targetHost = new IPEndPoint(Dns.GetHostAddresses("chat1.chat4free.de")[0], 8004);

            // Laenderchat
            //IPEndPoint targetHost = new IPEndPoint(Dns.GetHostAddresses("chat3.chat4free.de")[0], 8010);

            // Erotikchat
            //IPEndPoint targetHost = new IPEndPoint(Dns.GetHostAddresses("chat4.chat4free.de")[0], 8006);
            connectionArgs = new ConnectionArgs(targetHost);

            /*
            // elchat.com
            connectionArgs.AppletVersion = "2.7.1";
            connectionArgs.AppletUrl = "http://www.elchat.com/chat/";
            connectionArgs.Architecture = "x86";
            connectionArgs.JvmClassVersion = "5.0.0";
            connectionArgs.JvmVendor = "Sun Microsystems Inc.";
            connectionArgs.JvmVendorUrl = "http://java.sun.com/";
            connectionArgs.JvmVersion = "1.6.0_13";
            connectionArgs.Platform = "Windows Vista";
            connectionArgs.ServerUrl = "http://chat.chat4free.de/";
            connectionArgs.WindowsVersion = "6.0";
            */

            // Regionenchat
            connectionArgs.AppletVersion = "2.6.2.1";
            connectionArgs.AppletUrl = "http://www.chat4free.de/chateingang.html";
            connectionArgs.Architecture = "x86";
            connectionArgs.JvmClassVersion = "5.0.0";
            connectionArgs.JvmVendor = "Sun Microsystems Inc.";
            connectionArgs.JvmVendorUrl = "http://java.sun.com/";
            connectionArgs.JvmVersion = "1.6.0_13";
            connectionArgs.Platform = "Windows Vista";
            connectionArgs.ServerUrl = "http://chat.chat4free.de/";
            connectionArgs.WindowsVersion = "6.0";
            
            /*
            // Themenchat
            connectionArgs.AppletVersion = "2.6.2.1";
            connectionArgs.AppletUrl = "http://www.chat4free.de/chateingang.html";
            connectionArgs.Architecture = "x86";
            connectionArgs.JvmClassVersion = "50.0";
            connectionArgs.JvmVendor = "Sun Microsystems Inc.";
            connectionArgs.JvmVendorUrl = "http://java.sun.com/";
            connectionArgs.JvmVersion = "1.6.0_13";
            connectionArgs.Platform = "Windows Vista";
            connectionArgs.ServerUrl = "http://chat1.chat4free.de/";
            connectionArgs.WindowsVersion = "6.0";
            */

            /*
            //TODO: Different server version
            // Laenderchat
            connectionArgs.AppletVersion = "2.6.2.2";
            connectionArgs.AppletUrl = "http://www.chat4free.de/c4f_chateingang.html";
            connectionArgs.Architecture = "x86";
            connectionArgs.JvmClassVersion = "50.0";
            connectionArgs.JvmVendor = "Microsoft Corp.";
            connectionArgs.JvmVendorUrl = "http://java.sun.com/";
            connectionArgs.JvmVersion = "1.6.0_13";
            connectionArgs.Platform = "Windows Vista";
            connectionArgs.ServerUrl = "http://chat3.chat4free.de/international/";
            connectionArgs.WindowsVersion = "6.0";*/

            /*
            // Erotikchat
            connectionArgs.AppletVersion = "2.6.2.1";
            connectionArgs.AppletUrl = "http://www.erotikchat4free.de/chateingang.html";
            connectionArgs.Architecture = "x86";
            connectionArgs.JvmClassVersion = "50.0";
            connectionArgs.JvmVendor = "Microsoft Corp.";
            connectionArgs.JvmVendorUrl = "http://java.sun.com/";
            connectionArgs.JvmVersion = "1.6.0_13";
            connectionArgs.Platform = "Windows Vista";
            connectionArgs.ServerUrl = "http://chat4.chat4free.de/";
            connectionArgs.WindowsVersion = "6.0";*/

            #endregion

            this._volanoClient.Connect(connectionArgs);
        }

        private VolanoClient _volanoClient;

        void _volanoClient_ChannelListReceived(object sender, ChannelListEventArgs e)
        {
            foreach (var item in e.Channels)
            {
                this.channelsListView.Items.Add(item);
            }
        }

        void _volanoClient_Connected(object sender, EventArgs e)
        {
            this.OnLog(new LogEventArgs("Connected!"));
        }

        void _volanoClient_UserListReceived(object sender, UserListEventArgs e)
        {
            foreach (User item in e.Channel.Users)
            {
                this.usersListView.Items.Add(item);
            }
        }

        private void channelsListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Channel channel = this._volanoClient.CreateChannel(
                this.channelsListView.SelectedItem as string ?? "N/A",
                new IllSoft.Xvcc.Volano.Client.User(
                    this.nickTextBox.Text,
                    this.profileTextBox.Text));

            ChannelWindow channelWindow = new ChannelWindow(channel);

            this.OnNewDocumentRequired(new NewDocumentEventArgs(channelWindow));
        }

        private void channelsListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.usersListView.Items.Clear();

            this._volanoClient.SendGetUserList(this.channelsListView.SelectedItem as string);
        }

        protected virtual void OnLog(LogEventArgs e)
        {
            EventHandler<LogEventArgs> handler = this.Log;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnNewDocumentRequired(NewDocumentEventArgs e)
        {
            EventHandler<NewDocumentEventArgs> handler = this.NewDocumentRequired;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
}
